﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eSYSU.Business;

public partial class CommentFrom : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["ID"] != null && Request.QueryString["Title"] != null)
                ControlsInit(Request["ID"], Request["Title"]);
        }
    }

    //检查用户登录状态并设置相关控件属性
    protected void ControlsInit(string newsid, string newstitle)
    {
        if (Request.QueryString["ID"] != null)
        {
            lbl_NewsTitle.Text = newstitle;
            hl_ViewComments.NavigateUrl = "NewsComment.aspx?NewsID=" + newsid;

            int count = NewsCommentService.GetCommentCount(Convert.ToInt32(newsid));   //获取评论数
            if (count < 1)
            {
                hl_ViewComments.Text = "该新闻暂无评论。";
            }
            else
            {
                span_CommentCount.InnerText = count.ToString();   //显示评论数
            }
        }

        if (!eSYSU.Common.LocalStorage.CheckSession("eSYSU_Session_UserLogin"))
        {
            lbl_IsLogin.Text = "您尚未登录，请先<a href='SignIn.aspx'>登录</a>。";
            tb_Comment.Enabled = false;
            lbtn_PostComment.Enabled = false;
            lbtn_PostComment.ToolTip = "只有登录用户才能发表评论";
        }
    }

    //发表评论按钮事件
    protected void lbtn_PostComment_Click(object sender, EventArgs e)
    {
        if (eSYSU.Common.LocalStorage.CheckSession("eSYSU_Session_UserLogin"))
        {
            if (!string.IsNullOrEmpty(tb_Comment.Text.Trim()))
            {
                try
                {
                    eSYSU.Model.NewsComment comment = new eSYSU.Model.NewsComment();

                    comment.newsid = Convert.ToInt32(Request.QueryString["ID"].ToString());
                    comment.username = Session["eSYSU_Session_UserLogin"].ToString();
                    comment.posttime = DateTime.Now;
                    comment.content = tb_Comment.Text;

                    NewsCommentService.InserNewsComment(comment);
                    Response.Write(eSYSU.Common.StringHelper.JsCloseAndReloadParent("评论发布成功。",string.Empty));
                }
                catch (Exception ex)
                {
                    Response.Write(eSYSU.Common.StringHelper.JsIsNull("评论发布失败。"));
                }
            }
        }
        else
        {
            Response.Write(eSYSU.Common.StringHelper.JsIsReLoad("出错啦，您还没登录呢", "SignIn.aspx"));
        }
    }
}